package level4.net.third_party_api.uploaders
{
	import com.adobe.images.JPGEncoder;
	import com.adobe.images.PNGEncoder;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.system.Security;
	import flash.utils.ByteArray;
	
	import level4.net.third_party_api.renrenapi.RRResponseFormat;
	import level4.net.third_party_api.renrenapi.RRSignatureStruct;
	import level4.net.third_party_api.renrenapi.RRURLS;
	import level4.net.web.rfc1867impl.RFC1867ContentType;
	import level4.net.web.rfc1867impl.RFC1867Formatter;

	/**
	 * 人人平台,相片上传者
	 * */
	public class RRAlbumUploader
	{
		public var onOk:Function;
		public var onProcess:Function;
		
		public function RRAlbumUploader()
		{
		}
		
		/**
		 * 上次图片到 人人平台
		 * fileName : 人人平台 验证后缀.应该为 jpg,bmp,png.
		 * */
		public function uploadPhoto(struct:RRSignatureStruct,fileName:String,photoBitmapData:BitmapData):void{
			var sig:String = struct.getMD5Hash();
			
			var formatter:RFC1867Formatter = new RFC1867Formatter();
			
			var req:URLRequest = new URLRequest(RRURLS.URL_PHOTO);
				req.contentType = RFC1867Formatter.MultipartFormData;
				req.method = URLRequestMethod.POST;
				
				req.data = formatter.addValueBody({
					"v":struct.verson,
					"sig":sig,
					"api_key":struct.apiKey,
					"caption":struct.caption,
					"method":struct.method,
					"format":RRResponseFormat.RESP_JSON,
					"aid":struct.albumId,
					"call_id":struct.callId,
					"session_key":struct.sessionKey
				}).addFileBody(fileName,getBytes(photoBitmapData),RFC1867ContentType.IMAGE_X_PNG,"upload")
				.addTail();
			
			var loader:URLLoader = new URLLoader(req);
				loader.dataFormat = URLLoaderDataFormat.BINARY;
			addListeners(loader);
		}
		
		private function addListeners(loader:URLLoader):void {
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
		}
		
		private function removeListeners(loader:URLLoader):void {
			loader.removeEventListener(Event.COMPLETE, onComplete);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			loader.removeEventListener(ProgressEvent.PROGRESS, onProgress);
		}
		/**
		 * 获取 图片字节
		 * */
		private function getBytes(bmd:BitmapData):ByteArray {
			var bytes:ByteArray = new JPGEncoder(80).encode(bmd);
			bmd.dispose();
			return bytes;
		}
		////////////////////
		//事件
		////////////////////
		private function onComplete(e:Event):void {
			var l:URLLoader=e.target as URLLoader;
			removeListeners(l);
			if (onOk != null) {
				onOk(l.data);
			}
			onOk = null;
		}
		
		private function onIoError(e:IOErrorEvent):void {
			removeListeners(e.target as URLLoader);
		}
		
		private function onSecurityError(e:SecurityErrorEvent):void {
			removeListeners(e.target as URLLoader);
		}
		
		private function onProgress(e:ProgressEvent):void {
			if(onProcess != null){
				onProcess(e.bytesLoaded/e.bytesTotal);
			}
		}
		
	}
}